package lottery;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

import dao.DaoFactory;
import dao.User;
import dao.UserDao;

public class MyFrame extends JFrame {
	 public MyFrame() {
	        initComponents();
	        setTitle("彩票购买抽奖程序");
	        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
			int height = Toolkit.getDefaultToolkit().getScreenSize().height;
			//setSize(400,200);
			super.setLocation((width - 400) / 2, (height - 350) / 2);
			
			setVisible(true);
			this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	    }
	 
	 private void initComponents() {

		 JLabel label = new JLabel();
		 JLabel passwordLabel = new JLabel();
		 JTextField username = new JTextField();
		 JPasswordField password = new JPasswordField();
		 JLabel usernameLabel = new JLabel();
		 JButton  EnrollButton = new JButton();
		 JButton LoginButton = new JButton();

	        getContentPane().setBackground(Color.CYAN);

	        label.setFont(new Font("宋体", 1, 20)); // NOI18N
	        label.setText("搏一搏，单车变摩托");

	        passwordLabel.setText("Password:");
	        usernameLabel.setText("Username：");

	        EnrollButton.setText("注册");
	        
	        EnrollButton.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent e) {
	            	
	            	new RegisterFrame();
	                
	            }
	        });

	        LoginButton.setText("登录");
	        LoginButton.addActionListener(new ActionListener() {
	            @SuppressWarnings("deprecation")
				public void actionPerformed(ActionEvent e) {
	            	boolean flag = false;
	            
	            	User user =new User ();
	            	String username1 = username.getText();
	            	String password1 = password.getText();
	            	
	                UserDao userDao = DaoFactory.getUserDaoInstance();
	            	try {
						flag = userDao.Login(username1, password1);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
	            	
	            	if(flag){
	            		
	            		try {
							user = userDao.QuerryByUsername(username1);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
	            		
	            		new UserFrame(user);
	            	}
	            	else {
	            		JOptionPane.showMessageDialog(null, "账户或者密码错误！");
	            	}
	                
	            }
	        });
	        //ImageIcon con=new ImageIcon("C:\\Users\\罗俊\\Desktop\\b.jpg");
	        //ImagePanel panel=new ImagePanel(con);
	        //getContentPane().add(panel);
	        GroupLayout layout = new GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap(65, Short.MAX_VALUE)
	                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                    .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                        .addComponent(label)
	                        .addGap(91, 91, 91))
	                    .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
	                            .addComponent(EnrollButton)
	                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                                .addComponent(usernameLabel)
	                                .addComponent(passwordLabel, GroupLayout.PREFERRED_SIZE, 66,GroupLayout.PREFERRED_SIZE)))
	                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
	                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                            .addComponent(username, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
	                            .addComponent(password, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
	                        .addGap(79, 79, 79))
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(103, 103, 103)
	                        .addComponent(LoginButton)
	                        .addContainerGap(100, Short.MAX_VALUE))))
	        );
	        
	        layout.setVerticalGroup(
	                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
	                .addGroup(layout.createSequentialGroup()
	                    .addContainerGap()
	                    .addComponent(label)
	                    .addGap(18, 18, 18)
	                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	                        .addComponent(username, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	                        .addComponent(usernameLabel))
	                    .addGap(18, 18, 18)
	                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	                        .addComponent(passwordLabel, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
	                        .addComponent(password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
	                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
	                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
	                        .addComponent(EnrollButton)
	                        .addComponent(LoginButton))
	                    .addGap(20, 20, 20))
	            );

	            pack();
	            
	            
	            
}
}


